package com.chinaisafe.user.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chinaisafe.base.common.enums.UserTypeEnum;
import com.chinaisafe.gateway.client.auth.UaaAuthHelper;
import com.chinaisafe.module.user.request.RoleMenuReqVO;
import com.chinaisafe.module.user.response.SysRoleRespVO;
import com.chinaisafe.user.biz.SysRoleMenuBiz;
import com.chinaisafe.user.entity.SysRoleMenu;
import com.chinaisafe.user.entity.SysUser;
import com.chinaisafe.user.mapper.SysRoleMenuMapper;
import com.chinaisafe.user.mapper.SysUserMapper;
import com.chinaisafe.user.mapper.SysUserRoleMapper;
import com.chinaisafe.user.service.SysRoleMenuService;
import com.chinaisafe.user.service.SysUserRoleService;
import com.chinaisafe.user.util.MenuTreeBuilder;
import com.chinaisafe.user.vo.request.SysRoleMenuReqVO;
import com.chinaisafe.user.vo.response.SysMenuRespVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * SysRoleMenuBizImpl
 * Description：
 *角色菜单管理
 * @author tby
 * @date 2024/7/13 17:11
 */
@Service
public class SysRoleMenuBizImpl implements SysRoleMenuBiz {

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public List<SysMenuRespVO> allMenuTree(SysRoleMenuReqVO sysRoleMenuReqVO) {
        SysUser sysUser = sysUserMapper.selectById(UaaAuthHelper.getCurrentUserId());
        UserTypeEnum userTypeEnum = UserTypeEnum.getByCode(sysUser.getUserType());

        if (userTypeEnum != UserTypeEnum.ADMIN && userTypeEnum != UserTypeEnum.PLATFORM_ADMIN) {
            List<SysRoleRespVO> roles = sysUserRoleMapper.queryByUserId(sysUser.getId());
            List<Long> roleIds = roles.stream().map(SysRoleRespVO::getId).collect(Collectors.toList());
            sysRoleMenuReqVO.setMyRoleIds(roleIds);
        }
        List<SysMenuRespVO> sysMenus = sysRoleMenuMapper.queryAllMenu(sysRoleMenuReqVO);
        return MenuTreeBuilder.buildTrees(sysMenus);
    }

    @Override
    public List<SysMenuRespVO> myMenuTree(Long sysAppId, Long sysModuleId) {
        //获取我所有的角色
        List<Long> roleIds = sysUserRoleService.queryUserRole(UaaAuthHelper.getCurrentUserId());
        Map<String, Object> params = new HashMap<>(3);
        params.put("sysAppId", sysAppId);
        params.put("sysModuleId", sysModuleId);
        params.put("roles", roleIds);
        List<SysMenuRespVO> sysMenus = sysRoleMenuMapper.queryByParam(params);
        return MenuTreeBuilder.buildTrees(sysMenus);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void roleUpdateMenu(RoleMenuReqVO sysRoleMenuReqVO) {
        //先删除
        sysRoleMenuMapper.deleteByRoleId(sysRoleMenuReqVO.getSysRoleId());
        //再添加
        if (CollectionUtil.isEmpty(sysRoleMenuReqVO.getSysMenuIds())) {
            return;
        }
        List<SysRoleMenu> sysRoleMenus = new ArrayList<>(sysRoleMenuReqVO.getSysMenuIds().size());
        for (Long sysMenuId : sysRoleMenuReqVO.getSysMenuIds()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setSysRoleId(sysRoleMenuReqVO.getSysRoleId());
            sysRoleMenu.setSysMenuId(sysMenuId);
            sysRoleMenus.add(sysRoleMenu);
        }
        sysRoleMenuService.saveBatch(sysRoleMenus);
    }
}
